package vn.nichecafe.client;

import java.util.List;

import org.vaadin.gwtgraphics.client.shape.Path;

import vn.nichecafe.client.ui.slider.Pos;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class NICHECafeUtils {
	
	public static DialogBox createDialogBox(Widget widget) {
		// Create a dialog box and set the caption text
		final DialogBox dialogBox = new DialogBox();

		// Create a table to layout the content
		FlowPanel dialogContents = new FlowPanel();
		dialogBox.setWidget(dialogContents);

		// Add a close button at the bottom of the dialog
		Button closeButton = new Button("", new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});
		closeButton.setStyleName("close-button");
		dialogContents.add(closeButton);
		
		
		// Add an image to the dialog
		dialogContents.add(widget);
		
		dialogBox.setGlassEnabled(true);
		dialogBox.setAutoHideEnabled(true);
		dialogBox.center();

		// Return the dialog box
		return dialogBox;
	}
	/**
	 * 
	 * @param list - list of postion
	 * @param nameStyle - style of background change when hover
	 * @param panel - popup panel
	 * @param dialogContent
	 * @return
	 */
	public static Path createArea(List<Pos> list, final String nameStyle, final Widget panel, final Widget dialogContent ) {
		Path path = null;
		if(list != null){
			path = new Path(list.get(0).getX(),list.get(0).getY());
			for (Pos pos : list) {
				path.lineTo(pos.getX(), pos.getY());
			}
			path.close();
			path.setFillOpacity(0);
			path.setStrokeOpacity(0);
			path.setStyleName("area-hover");
			path.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					panel.removeStyleName(nameStyle);
					DialogBox dialogBox = NICHECafeUtils.createDialogBox(dialogContent);
					dialogBox.show();
				}
			});
			path.addMouseOverHandler(new MouseOverHandler() {
				
				@Override
				public void onMouseOver(MouseOverEvent event) {
					panel.addStyleName(nameStyle);
				}
			});
			path.addMouseOutHandler(new MouseOutHandler() {
				
				@Override
				public void onMouseOut(MouseOutEvent event) {
					panel.removeStyleName(nameStyle);
				}
			});
		}
		return path;
	}
}
